package vashion.azeroth.api.controller.member;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import vashion.azeroth.api.controller.base.BaseController;
import vashion.azeroth.api.result.Result;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.core.personal.pojo.PersonalProfiles;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.PersonalProfilesService;

@Controller
@RequestMapping("/member")
public class ProfilesController<T> extends BaseController {

	@Autowired
	private PersonalProfilesService<T> profilesService;

	@RequestMapping("addProfiles.htm")
	@ResponseBody
	public Result<T> addProfiles(
			@ModelAttribute("profiles") PersonalProfiles profiles) {
		return null;
	}

	@RequestMapping("getProfiles.htm")
	@ResponseBody
	public ApiResult<PersonalProfiles> getProfiles(
			@ModelAttribute("profiles") PersonalProfiles profiles) {
		ApiResult<PersonalProfiles> resProfilesList = new ApiResult<PersonalProfiles>();
		try {
			if (null == profiles.getMemberId()
					|| StringUtils.isBlank(String.valueOf(profiles
							.getMemberId()))) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.PARAMETER_ERROR.getErrorCode(),
						GenericErrorEnum.PARAMETER_ERROR.getErrorMessage());
			} else {
				resProfilesList = profilesService.getProfiles(profiles);
			}

		} catch (Exception e) {

			return ApiResult.newErrorResult(
					GenericErrorEnum.PARAMETER_ERROR.getErrorCode(),
					GenericErrorEnum.PARAMETER_ERROR.getErrorMessage());
		}
		return resProfilesList;
	}

	@RequestMapping("updateProfiles.htm")
	@ResponseBody
	public ApiResult<T> updateProfiles(
			@ModelAttribute("profiles") PersonalProfiles profiles) {
		ApiResult<T> updateRes = new ApiResult<T>();
		
		try {
			if (null == profiles.getMemberId()
					|| StringUtils.isBlank(String.valueOf(profiles
							.getMemberId()))) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.PARAMETER_ERROR.getErrorCode(),
						GenericErrorEnum.PARAMETER_ERROR.getErrorMessage());
			} else {
				updateRes = profilesService.updProfiles(profiles);
			}
		} catch (Exception e) {
			return ApiResult.newErrorResult(
					GenericErrorEnum.PARAMETER_ERROR.getErrorCode(),
					GenericErrorEnum.PARAMETER_ERROR.getErrorMessage());
		}
		return updateRes;
	}

}
